package cn.wolfcode.wolf2w.job;


import cn.wolfcode.wolf2w.redis.IStrategyRedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 将缓存中的数据持久化到数据库
 */

@Component
public class RedisDataPersistenceJob {

    @Autowired
    private IStrategyRedisService  strategyRedisService;

    @Autowired
    private IStrategyService strategyService;

    // 实现持久化逻辑的方法
    @Scheduled(cron = "0/30 * * * * ?")
    void doWork(){
        System.out.println("====================");
        // 将数据(vo对象)从redis中取出来
        List<StrategyStatisVO> vos = strategyRedisService.getStrategyStatisVos();
        for (StrategyStatisVO vo : vos) {
            // 更新数据库里的数据
            strategyService.updateStrategyVo(vo);
        }
        System.out.println("持久化-------------");
    }
}
